<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Aplicantes</title>

<link rel="stylesheet"
	href="<%=request.getContextPath()%>/style/gen-style.css"
	type="text/css" />
<link rel="stylesheet"
	href="<%=request.getContextPath()%>/style/mkv/jquery-ui.css"
	type="text/css" />
<link rel="stylesheet"
	href="<%=request.getContextPath()%>/style/ui.jqgrid.css"
	type="text/css" />
<link rel="stylesheet"
	href="<%=request.getContextPath()%>/style/makovo-style.css"
	type="text/css" />

<script type="text/javascript"
	src="<%=request.getContextPath()%>/scripts/jquery-2.1.3.js"></script>
<script type="text/javascript"
	src="<%=request.getContextPath()%>/scripts/gen.js"></script>
<script type="text/javascript"
	src="<%=request.getContextPath()%>/scripts/inegi.js"></script>
<script type="text/javascript"
	src="<%=request.getContextPath()%>/scripts/ui/jquery.jqGrid.src.js"></script>
<script type="text/javascript"
	src="<%=request.getContextPath()%>/scripts/spin.js"></script>
<script type="text/javascript"
	src="<%=request.getContextPath()%>/scripts/auxiliares.js"></script>

<script type="text/javascript">
	if (validaSesion("${sessionScope.LoginMenu}"))
		document.location = "index.jsp";
	var mPageSize = 50;
	var mTotal = 100;

	var mCurrentFilter = {
		"curp" : null,
		"state" : -1,
		"city" : -1,
		"step" : -1,
		"page" : 0,
		"change" : 1
	}

	$(document)
			.ready(
					function() {
						if (validaSesion("${sessionScope.LoginMenu}"))
							document.location = "index.jsp";
						var mCall = new Gen.AjaxCall();
						var mApplicants;
						var mTemplate = "<table><tr><td>Domicilio:</td><td>{0}</td></tr></table>";

						function setHeader(p) {

							var end = p.pageSize * (mCurrentFilter.page + 1);

							mTotal = p.total;
							mPageSize = p.pageSize;

							if (end > mTotal)
								end = mTotal;

							$('#num-end').text(end);
							$('#num-init').text(p.pageSize * mCurrentFilter.page +1);
							$('#num-total').text(p.total);
						}

						$('#filter').click(function() {
							mCurrentFilter.curp = $('#curp').val();
							mCurrentFilter.step = $('#step').val();
							mCurrentFilter.state = $('#state').val();
							mCurrentFilter.city = $('#city').val();
							mCurrentFilter.page = 0;
							mCurrentFilter.change = 1;

							getApplicants(mCurrentFilter, refreshGrid);
							mCurrentFilter.change = 0;
						});

						$('#state').change(function() {
							var stateId = $(this).val();
							var state = findValue(Inegi.states, 'id', stateId);

							if (stateId != -1)
								Inegi.fillCities($('#city'), stateId, true);
						});

						$('#prev').click(function() {
							if (mCurrentFilter.page <= 0)
								return;

							mCurrentFilter.curp = $('#curp').val();
							mCurrentFilter.step = $('#step').val();
							mCurrentFilter.state = $('#state').val();
							mCurrentFilter.city = $('#city').val();
							mCurrentFilter.page = mCurrentFilter.page - 1;
							mCurrentFilter.change = 0;

							getApplicants(mCurrentFilter, refreshGrid);
						});

						$('#next').click(function() {
							if (((mCurrentFilter.page + 1) * mPageSize) >= mTotal)
								return;

							mCurrentFilter.curp = $('#curp').val();
							mCurrentFilter.step = $('#step').val();
							mCurrentFilter.state = $('#state').val();
							mCurrentFilter.city = $('#city').val();
							mCurrentFilter.page = mCurrentFilter.page + 1;
							mCurrentFilter.change = 0;

							getApplicants(mCurrentFilter, refreshGrid);
						});

						$('#tblUsers').jqGrid(
								{
									datatype : "local",
									height : "auto",
									width : 1000,
									caption : "Listado de clientes",
									rowNum : 1000,
									rowList : [ 1000, 2000 ],
									colNames : [ 'CURP', 'Nombre', 'Documento', 'Proceso', '', '' ],
									rowattr : function(row, rowel) {
										if (rowel.appStep == 102)
											return {
												"class" : "approved"
											};
										if (rowel.appStep == 103)
											return {
												"class" : "rejected"
											};
									},
									colModel : [ {
										name : 'curp',
										index : 'curp',
										width : 100,
										key : true
									}, {
										name : 'name',
										index : 'name',
										width : 200,
										formatter : formatterApplicant
									}, {
										name : 'doc',
										index : 'doc',
										width : 55,
										formatter : formatterDocument
									}, {
										name : 'continue',
										index : 'continue',
										width : 50,
										formatter : formatterContinue
									}, {
										name : 'edit',
										index : 'edit',
										width : 32,
										formatter : formatterEdit
									}, {
										name : 'delete',
										index : 'delete',
										width : 35,
										formatter : formatterDelete
									} ],
									subGrid : true,
									subGridRowExpanded : function(subgrid_id, row_id) {
										var row = findValue(mApplicants, "curp", row_id);
										var temp = mTemplate.format(row.address.street + " " + row.address.extNum + " "
												+ row.address.intNum + "<br/>" + row.address.state + " " + row.address.city
												+ ", " + row.address.loc + "<br/>C.P.: " + row.address.cp + " <br/>");
										$('#' + subgrid_id).append(temp);

									},
									loadComplete : function(request) {

										$('.continue').click(function() {
											var id = this.id.split('_')[1];
											navigateContinue(id);
										});

										$('.show-doc').click(function() {
											var id = this.id.split('_')[1];
											navigateDetails(id);
										});

										$('.edit').click(function() {
											if ("${sessionScope.LogintipoUsr}" == "1" || "${sessionScope.LogintipoUsr}" == "4") {
												var id = this.id.split('_')[1];
												editApplicant(id);
											} else
												alert("No tiene permisos para editar perfil");
										});

										$('.delete').click(function() {
											if ("${sessionScope.LogintipoUsr}" == 1) {
												var id = this.id.split('_')[1];
												deleteApplicant(id);
											} else
												alert("No tiene permisos para editar perfil");
										});
									}
								});

						function refreshGrid(data) {
							$('#tblUsers').jqGrid('clearGridData');
							$('#tblUsers').jqGrid('setGridParam', {
								data : data
							});
							$('#tblUsers').trigger('reloadGrid');
						}

						function formatterApplicant(cellValue, options, rowObject) {
							return rowObject.name + " " + rowObject.lastName + " " + rowObject.secondLastName;
						}

						function formatterDocument(cellValue, options, rowObject) {
							return "<button class='t-btn change tiny show-doc' id='show_"+ rowObject.curp +"' type='button'>Mostrar</button>";
						}

						function formatterContinue(cellValue, options, rowObject) {
							return "<button class='t-btn change tiny continue' id='continue_"+ rowObject.curp +"' type='button'>Continuar</button>";
						}

						function formatterEdit(cellValue, options, rowObject) {
							return "<button class='t-btn change tiny edit' id='edit_"+ rowObject.curp +"' type='button'>Editar</button>";
						}

						function formatterDelete(cellValue, options, rowObject) {
							return "<button class='t-btn change tiny delete' id='delete_"+ rowObject.curp +"' type='button'>Borrar</button>";
						}

						function navigateDetails(id) {
							var data = {
								id : id
							};

							mCall.post('ajax/NavigateDetails.action', data, function(a) {
								document.location = "../paginas/detalle.jsp";
							}, function(er) {

							});
						}

						function navigateContinue(id) {
							var data = {
								id : id
							};

							mCall.post('ajax/NavigateContinue.action', data, function(a) {
								document.location = a.newLoc;

							}, function(er) {

							});
						}

						function deleteApplicant(id) {
							var data = {
								id : id
							};
							/*llamar a pagina de borrado,similar a detalle*/
							mCall.post('ajax/NavigateDetails.action', data, function(a) {
								document.location = "../paginas/deleteApplicant.jsp";
							}, function(er) {

							});
						}

						function editApplicant(id) {
							var data = {
								id : id
							};

							mCall.post('ajax/NavigateDetails.action', data, function(a) {
								document.location = "../tareas/editar_perfil.jsp";
							}, function(er) {

							});
						}

						function getApplicants(filter, afterFunction) {
							showOverlay(true);
							mCall.post('ajax/GetApplicants.action', {
								id : filter.curp,
								step : filter.step,
								state : filter.state,
								city : filter.city,
								page : filter.page,
								change : filter.change
							}, function(a) {
								mApplicants = a.applicants;
								if (afterFunction != null)
									afterFunction(mApplicants);

								setHeader(a);
								hideOverlay();
							}, function(er) {
								hideOverlay();
							});
						}

						function getUserInfo() {
							mCall.post('ajax/GetUser.action', {}, function(u) {
								var nada = u;

							}, function(er) {
							});
						}

						function setFilters(type) {
							if (parseInt(type) == 4) {
								$('#state').parent().parent().hide();
								$('#city').parent().parent().hide();
							}

						}

						Inegi.fillStates($('#state'));
						setFilters("${sessionScope.LogintipoUsr}");
						getApplicants(mCurrentFilter, refreshGrid);
						getUserInfo();
					});
</script>
</head>
<body>
	<div style="width: 1100px; margin: 0 auto">
		<form action="ajax/GetUser">
			<div class="headerMK">
				<div class="logo">
					<span class="hdTitulo">Sistema para Aplicación de Subsidio
						2015</span>
				</div>
				<div
					style="font-size: 0.26em; top: -50px; position: relative; width: 500px; float: right; text-align: right; padding-right: 10px">
					<i>${sessionScope.LoginNombre}</i><b><a
						href="/makovo/paginas/index.jsp"> - Cerrar Sesión</a></b>
				</div>
			</div>
		</form>

		<div class="divContenido">
			<a href="<%=request.getContextPath()%>/paginas/menu.jsp">Regresar
				al menú</a> <br /><br/>∫
			<div style="padding-left: 100px">
				<table style="width: 100%">
					<tr>
						<td><input type="text" placeholder="CURP" class="t-box"
							t-box-type="user" id="curp"></td>
						<td><select class="t-box" id="state"></select></td>
						<td><select class="t-box" id="city">
								<option value="-1">.. Seleccione una opción ..</option>
						</select></td>
						<td><select class="t-box" id="step">
								<option value="-1">.. Seleccione una opción ..</option>
								<option value="102">Aprobado</option>
								<option value="103">Rechazado</option>
						</select></td>

						<td style="width: 20%">
							<form action="ajax/GetApplicants">
								<button type="button" id="filter" class="t-btn tiny hor-opening">Filtrar</button>
							</form>
						</td>


					</tr>
				</table>
			</div>

			<div style="width: 1000px; margin: 0px auto; padding: 2em;">
				<div style="float: right;">
					<span>Se muestran registros </span> <span id="num-init">1</span> <span>a</span>
					<span id="num-end"></span> <span>de</span> <span id="num-total"></span>
				</div>
				<br> <br>
				<table id="tblUsers">

				</table>
				<br> <br>
				<div>
					<button type="button" style="float: left;" id="prev"
						class="t-btn tiny hor-opening">Página anterior</button>
					<button type="button" style="float: right;" id="next"
						class="t-btn tiny hor-opening">Página siguiente</button>
				</div>
				<br> <br> <br>
			</div>
		</div>
	</div>

</body>
</html>